package com.boe.csb.core.service.user;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boe.csb.core.common.exception.CSBErrorCode;
import com.boe.csb.core.dao.BoeAppMapper;
import com.boe.csb.core.dao.BoeUserFtpMapper;
import com.boe.csb.core.entity.po.BoeAppBean;

@Service("boeAppService")
public class BoeAppService {

	@Autowired
	private BoeAppMapper boeAppMapper;
	@Autowired
	private BoeUserFtpMapper boeUserFtpMapper;
	
	/**
	 * 新增app发布
	 * @param boeApp
	 * @return
	 */
	public long addAppInfo(BoeAppBean boeApp,String sql){
		boeAppMapper.addAppInfo(boeApp);
		try{
			boeUserFtpMapper.updateByAppId(boeApp.getId(),sql);
		}catch(Exception e){
			e.printStackTrace();
		}
		return boeApp.getId();
	}
	
	/**
	 * 删除app
	 * @param boeApp
	 * @return
	 */
	public int deleteAppInfo(Long id){
		return boeAppMapper.deleteAppInfo(id);
	}
	
	/**
	 * 修改app
	 * @param boeApp
	 * @return
	 */
	public int updateAppInfo(BoeAppBean boeApp,String sql){
		int num = boeAppMapper.updateAppInfo(boeApp);
		boeUserFtpMapper.updateByAppId(boeApp.getId(),sql);
		return num;
	}
	
	
	/**
	 * 查询app列表
	 * @param boeApp
	 * @return
	 */
	public List<BoeAppBean> loadAppInfo(Integer pageNum,Integer limit,String display){
		if(display != null && "1".equals(display)){
			return boeAppMapper.loadAppInfo((pageNum-1)*limit,limit,Long.valueOf(1));
		}
		return boeAppMapper.loadAppInfo((pageNum-1)*limit,limit,null);
	}
	
	/**
	 * 单条查询app
	 * @param boeApp
	 * @return
	 */
	public BoeAppBean getAppInfo(Long id){
		return boeAppMapper.getAppInfo(id);
	}
	
	public CSBErrorCode displayAppInfo(Long id,Long display){
		int i = boeAppMapper.putDisplay(id,display);
		if(i == 1){
			return CSBErrorCode.SUCCESS;
		}else{
			return CSBErrorCode.SYS_EXP;
		}
		
	}
	
	public long countApp(String display){
		if(display != null && "1".equals(display)){
			return boeAppMapper.countApp(Long.valueOf(1));
		}
		return boeAppMapper.countApp(null);
	}
	
}
